package com.xry.controller;

import com.xry.bean.Product;
import com.xry.service.ProductService;
import com.xry.util.Page;
import com.xry.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin//允许跨域
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;

    @PostMapping
    public Result add(@RequestBody Product product){
        Object obj=productService.add(product);
        return Result.success("新增成功",obj);
    }

    @PutMapping
    public Result edit(@RequestBody Product product){
        Object obj=productService.edit(product);
        return Result.success("修改成功",obj);
    }

    @DeleteMapping("{id}")
    public Result del(@PathVariable("id") Integer id){
        Object obj=productService.del(id);
        return Result.success("删除成功",obj);
    }

    @GetMapping("{id}")
    public Result one(@PathVariable("id")Integer id){
        Object obj=productService.one(id);
        return Result.success(obj);
    }

    @GetMapping("all")
    public Result all( Product product){
        Object obj=productService.all(product);
        return Result.success(obj);
    }
    @GetMapping("page")
    public Result page(Page page, Product product){
        //查询一共有多少条数据
        //查询当前页要显示的数据
        //将当前页封装到page对象中
        productService.page(page,product);
        //返回page对象
        return Result.success(page);
    }

    @GetMapping("cbrand")
    public Result countBrand(){
        Object obj=productService.countBrand();
        return Result.success(obj);
    }
    @GetMapping("ccategory")
    public Result countCategory(){
        Object obj=productService.countCategory();
        return Result.success(obj);
    }

}
